package sb.tiledui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader.TileMode;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author SeungMin 타일이 되는 view, 박스라고도 합니다.
 */
public class Tile extends View {

	public static int NUM_OF_BOX = 25;   
	public static int NUM_OF_BOXES_IN_STAGE = 4;  
	
	public static int DISPLAY_WIDTH; //화면 가로 길이  

	// 최소 작은 박스에서 최대 큰 박스까지 총 30개의 level로 이루어져있다.
	// 한 레벨 차이는 최대 큰 박스와 최소 큰 박스의 차이를 30으로 나눈 값
	public static int LEVEL_NUM = 30;


	double weight; // Factor that determines the area of his view
	int color1; // Color of this View
	int color2;
	public int width; // 가로 길이
	public int height; // 세로 길이
	int level; // 사이즈 레벨
	int isHorizontal;// level 2가 아니면 0, level 2면서 가로로 길면 1 세로로 길면 2
	
	
	public Bitmap image; //image
	
	boolean isTouched;
	boolean hasPosition; // not used
	
	GroupPurchase gp;
	public TiledUIActivity parent;

	public Tile(Context context) {
		super(context);

		weight = 0;
		hasPosition = false;		
		level = -1;
		isTouched = false;
		
		image = null;
		
		gp = null;
		parent = null;
		
	}

	public void onDraw(Canvas canvas) {

		
		Paint pnt = new Paint();
		pnt.setAntiAlias(true);
		pnt.setShader(new LinearGradient(0,0,this.width,this.height,color1,color2, TileMode.CLAMP));
		canvas.drawRect(0,0,this.width,this.height,pnt);
		
		
		if(image != null){
			Paint pntString = new Paint();
			Paint pntStringBackground = new Paint();
			
			pntStringBackground.setColor(Color.BLACK);
			pntStringBackground.setAlpha(98);
			
			pntString.setAntiAlias(true);
			pntString.setColor(Color.WHITE);
			//pntString.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
			pntString.setTextSize(20);
			String title = gp.title;
			
			if(this.level == 3 ){
				int w = this.width;
				int h = this.height;
				Rect dst = new Rect(0, 0, w, h);
				canvas.drawBitmap(image, null, dst, null);
				canvas.drawRect(0, h-50, w, h, pntStringBackground);
				canvas.drawText(title, 5, h-20, pntString);
				
			}else if(this.level == 2){
				int w = this.width;
				int h = this.height;
				Rect dst = null;
				if(this.width > this.height){
					dst = new Rect(0, 0, w/2, h);
					canvas.drawText(title, w/2+5, h/2, pntString);
				}else{
					dst = new Rect(0, 0, w, h/2);	
					canvas.drawText(title, 5, h/2+25, pntString);			
				}
				canvas.drawBitmap(image, null, dst, null);
			}else if(this.level ==1){
				int w = this.width;
				int h = this.height;
				Rect dst = new Rect(0, 0, w, h);
				canvas.drawBitmap(image, null, dst, null);		
			}else{
				Log.i("SM","error 2");
			}
		}
		
		super.onDraw(canvas);
	}

	public void setWeight(double weight) { 
		this.weight = weight;
	}
	
	public int calculateSizeOfView(int marginSize){
		if(level == 3){
			this.width = ((DISPLAY_WIDTH - 6*marginSize)/NUM_OF_BOXES_IN_STAGE)*2;
			this.height = this.width + marginSize;
		}else if(level ==2){
			this.height = ((DISPLAY_WIDTH - 6*marginSize)/NUM_OF_BOXES_IN_STAGE)*2 + marginSize;
			this.width = (DISPLAY_WIDTH - 6*marginSize)/NUM_OF_BOXES_IN_STAGE;
		}else{
			this.width = this.height = (DISPLAY_WIDTH - 6*marginSize)/NUM_OF_BOXES_IN_STAGE;
		}
		
		return 1;
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		
		int action = event.getAction();
		switch(action){
		case MotionEvent.ACTION_DOWN:
			isTouched = true;
			break;
		case MotionEvent.ACTION_UP:

			parent.changeView(TiledUIActivity.SUB_TILE_FRONT, gp);
			isTouched = false;  
			break;

		default: 
			isTouched = false;
			break;
		}
        return true;
     }
	 
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(this.width, this.height);
	}

}
